<template>
  <div id="all">
    <!-- 加载中提示 -->
    <van-loading v-if="isLoading && allListData.length === 0" type="spinner" color="#1989fa" vertical>
      加载中...
    </van-loading>

    <!-- 无数据提示 -->
    <van-empty v-else-if="!isLoading && allListData.length === 0 && searchQuery" description="暂无数据" />

    <!-- 数据列表 -->
    <div class="all-container" v-if="allListData.length > 0">
      <div class="all-item" v-for="item in allListData" :key="item.title">
        <div class="category-name" v-if="item.data != 0">{{ item.title }}</div>
        <div class="category-list">
          <div class="list-item" v-for="data in item.data" :key="data.id">
            <div class="item-img" :style="{ backgroundImage: 'url(' + data.cover + ')' }"></div>
            <div class="item-info">
              <div class="info-title">{{ data.title }}</div>
              <div class="info-desc">{{ data.subtitle }}</div>
            </div>
          </div>
        </div>
        <div class="more" v-if="item.title !== '作者/音乐人'">查看更多>> </div>
      </div>
    </div>
  </div>
</template>


<script>
import { mapState } from 'vuex';
import { searchByCategory } from '@/api/api';

export default {
  data() {
    return {
      category: [
        { name: 'author', title: '作者/音乐人' },
        { name: 'hp', title: '日签' },
        { name: 'reading', title: '阅读' },
        { name: 'radio', title: '收音机' },
        { name: 'music', title: '音乐' },
        { name: 'movie', title: '书影' }
      ],
      allListData: [],
      isLoading: false
    }
  },
  computed: {
    ...mapState(['searchQuery'])
  },
  watch: {
    // 监听搜索关键词变化，重新请求数据
    searchQuery: {
      immediate: true, // 初始加载时执行一次
      handler() {
        this.fetchAllData();
      }
    }
  },
  methods: {
    async fetchAllData() {
      if (!this.searchQuery) {
        this.allListData = [];
        this.isLoading = false;
        return;
      }

      this.isLoading = true;

      try {
        // 创建所有请求的Promise数组
        const requests = this.category.map(item => {
          return searchByCategory(item.name, this.searchQuery)
            .then(res => {
              // 返回格式化后的对象
              return {
                title: item.title,
                data: res.data?.list || []
              };
            });
        });

        // 等待所有请求完成
        this.allListData = await Promise.all(requests);

      } catch (error) {
        console.error('请求数据失败:', error);
        this.allListData = [];
      } finally {
        this.isLoading = false;
      }

    }

  }
}
</script>

<style scoped lang="less">
#all {
  width: 100vw;
  height: 100vh;
  box-sizing: border-box;
  overflow-x: hidden;
}

.all-container {
  width: 100%;
  padding: 0 1vw;
  box-sizing: border-box;
  background-color: #fff;

  .all-item {
    width: 100%;
    margin-bottom: 1vh;

    &:nth-child(1) .list-item .item-img {
      border-radius: 50%;
    }

    .more {
      width: 100%;
      display: flex;
      justify-content: center;
      font-size: 10px;
      color: #ccc;
      margin-bottom: 2vh;
      white-space: nowrap;
    }
  }

  .category-name {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    color: #ccc;
    margin-bottom: 3vh;
    white-space: nowrap;

    &::before {
      content: '';
      width: 45%;
      height: 1px;
      transform: scaleY(0.2);
      background-color: #b9b9b9;
      margin-right: 5px;
    }

    &::after {
      content: '';
      width: 45%;
      height: 1px;
      transform: scaleY(0.2);
      background-color: #b9b9b9;
      margin-left: 5px;
    }
  }

  .category-list {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 2vh;
    margin-bottom: 3vh;

    .list-item {
      width: 100%;
      height: 6vh;
      display: flex;
      align-items: center;
      gap: 2vw;
      padding-bottom: 2vh;
      margin-bottom: 2vh;
      border-bottom: 1px solid #f5f5f5;

      .item-img {
        height: 100%;
        aspect-ratio: 1/1;
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
      }

      .item-info {
        width: 100%;
        font-size: 12px;
        height: 90%;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .info-title {
          width: 80%;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        .info-desc {
          font-size: 8px;
          color: #bbb;
        }
      }

    }
  }
}
</style>